__author__ = 'Administrator'

Field_Name_Row = 0
Field_Type_Row = 1
Field_Desc_Row = 2
Field_Param_Row = 3
Data_Start_Row = Field_Param_Row + 1


class FieldInfo:
    def __init__(self, loader, col_index):
        self.col_index = col_index
        self.loader = loader
        self.name = self.loader.get_string(Field_Name_Row, self.col_index)
        self.type = self.loader.get_string(Field_Type_Row, self.col_index)
        self.desc = self.loader.get_string(Field_Desc_Row, self.col_index)
        self.param = self.loader.get_string(Field_Param_Row, self.col_index)

    def get_col_type(self, col_index):
        return self.loader.get_string(Field_Type_Row, col_index)

    def get_type(self):
        return self.type

    def get_len(self):
        return 1

    def get_name(self):
        return self.name

    def get_desc(self):
        return self.desc

    def get_col_index(self):
        return self.col_index

    def __str__(self):
        return '{name = %s,type = %s}' % (self.name, self.type)


class ArrayFiledInfo(FieldInfo):
    def __init__(self, loader, col_index):
        FieldInfo.__init__(self, loader, col_index)
        self.params = self.param.split('*')
        self.fields = []
        self.generate_child_fields()

    def get_start_index(self):
        return self.col_index + 1

    def get_end_index(self):
        return self.col_index + self.get_len()

    def get_len(self):
        return self.get_arr_count() * self.get_arr_child_count() + 1

    def get_arr_count(self):
        return int(self.params[0])

    def get_arr_child_count(self):
        return int(self.params[1])

    def generate_child_fields(self):
        for arr_index in range(self.get_arr_count()):
            start_index = self.get_start_index() + arr_index * self.get_arr_child_count()
            child_start_index = 0
            struct_fields = []
            while child_start_index < self.get_arr_child_count():
                if self.loader.is_empty(Field_Type_Row, start_index + child_start_index):
                    child_start_index += 1
                    continue
                field_type = self.get_col_type(start_index + child_start_index)
                if field_type == "array":
                    field_info = ArrayFiledInfo(self.loader, start_index + child_start_index)
                else:
                    field_info = FieldInfo(self.loader, start_index + child_start_index)
                struct_fields.append(field_info)
                child_start_index += field_info.get_len()
            self.fields.append(struct_fields)

    def get_fields(self):
        return self.fields

    def __str__(self):
        field_str = ""
        for struct_fields in self.fields:
            field_str += '{'
            for field in struct_fields:
                field_str += field.__str__()
            field_str += '}\n'
        return '{name = %s,type = %s,fields = \n%s}' % (self.name, self.type, field_str)


class SheetParser:
    def __init__(self):
        self.loader = None
        self.fields = []
        self.datas = []

    def get_col_type(self, col_index):
        return self.loader.get_string(Field_Type_Row, col_index)

    def parse(self, loader):
        self.loader = loader
        self.parse_field()
        self.parse_data()
        return True

    def get_sheet_name(self):
        return self.loader.get_sheet_name()

    '''
    生成字段，并将字段和列索引关联起来
    '''
    def parse_field(self):
        col_index = 0
        while col_index < self.loader.get_num_cols():
            if self.loader.is_empty(Field_Type_Row, col_index):
                col_index += 1
                continue
            field_type = self.get_col_type(col_index)
            if field_type == "array":
                field_info = ArrayFiledInfo(self.loader, col_index)
            else:
                field_info = FieldInfo(self.loader, col_index)
            self.fields.append(field_info)
            col_index += field_info.get_len()

    '''
    生成临时数据
    '''
    def parse_data(self):
        for row_index in range(Data_Start_Row,self.loader.get_num_rows()):
            row_data = []
            for col in range(self.loader.get_num_cols()):
                if self.get_col_type(col) == 'array':
                    row_data.append('')
                elif self.get_col_type(col) == 'string':
                    row_data.append(self.loader.get_string(row_index, col))
                elif self.get_col_type(col) == 'int32':
                    row_data.append(self.loader.get_int(row_index, col))
                elif self.get_col_type(col) == 'lua':
                    row_data.append([self.loader.get_string(row_index, col)])
                else:
                    pass

            self.datas.append(row_data)

    def get_fields(self):
        return self.fields

    def get_data(self):
        return self.datas